/**
 * Version 17 mrt 2008 - initial version
 */
package audioreaper.tools;

/**
 * <code>Note</code>.
 *
 * @author <a href="mailto:vincent@nazarene.nl">Vincent van Beveren</a>
 *
 */
public class Tone {
	
	private static final float[] MIDI_TABLE = new float[128];
	
	static {
		for (int i = 0; i < MIDI_TABLE.length; i++) {
			MIDI_TABLE[i] =
				(float)Math.pow(2, (float)(i - 69) / 12) * 440;
		}
	}
	
	public static final int MAX_NOTE = 127;
	public static final int MIN_NOTE = 0;
	
	public enum Note {
		C(0), Cs(1), 
		Df(1), D(2), Ds(3), 
		Ef(3), E(4), 
		F(5), Fs(6),
		Gf(6), G(7), Gs(8),
		Af(8), A(9), As(10),
		Bf(10), B(11);
		
		private int _toneNr;
		
		private Note(int toneNr) {
			_toneNr = toneNr;
		}
		
	}
	
	
	
	/**
	 * Returns the MIDI number belonging to the specific note.
	 */
	public static final int getMidi(Note note, int octave) {
		return (octave + 1) * 12 + note._toneNr; 		
	}
	
	/**
	 * Returns the frequency of a specific note.
	 */
	public static final float getFrequency(Note note, int octave, int cents) {
		if (cents == 0 && octave >= -1 && octave <= 9) {
			return MIDI_TABLE[getMidi(note, octave)];			
		}
		float n = getMidi(note, octave);
		n += 0.001 * cents; 
		return (float)Math.pow(2, (n - 69) / 12) * 440;
	}
	
	public static void main(String[] args) {
		System.out.println(getMidi(Note.C, 5));
		System.out.println(getFrequency(Note.C, 5, 0));
	}
	
}
